from fastapi import APIRouter, Depends

from app.core.responses import Response
from modules.admin.services.auth import get_current_user
from schemas.category import Create, Update
from app.services.category import CategoryServices

router = APIRouter(prefix="/category", dependencies=[Depends(get_current_user)])


@router.get("/",summary="获取列表")
async def index():
    data = CategoryServices.get_all().serialize()
    return Response.success(data)


@router.post("/",summary="创建")
async def store(form: Create):
    data = CategoryServices.create(form)
    return Response.success(data.serialize())


# @router.get("/{Id}",summary="详情")
# async def show(Id: int):
#     data = CategoryServices.get_one(Id).serialize()
#     poster = data['poster'][0]
#     desc_image = data['desc_image'][0]
#     data['desc_image'] = [{"url":desc_image}]
#     data['poster'] = [{"url":poster}]
#     return Response.success(data)


@router.patch("/{category_id}",summary="更新")
async def update(form: Update,category_id:str):
    data = CategoryServices.update(form,category_id)
    return Response.success(data.serialize())


@router.delete("/{Id}",summary="删除")
async def delete(Id: int):
    try:
        CategoryServices.delete(Id)
    except Exception as e:
        return Response.success({"message": "删除失败"})
    return Response.success({"message": "删除成功"})


